package com.itheima.common;

import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletRequest;
import java.time.LocalDateTime;


@Component
@Slf4j
public class MyMetaObjectHandler implements MetaObjectHandler {
    //    在插入的时候才会使用
    @Override
    public void insertFill(MetaObject metaObject) {
        log.info("公共字段填冲insert");
        log.info(metaObject.toString());
        metaObject.setValue("createTime",LocalDateTime.now());
        metaObject.setValue("updateTime",LocalDateTime.now());
//        这个会因为不同线程id而有不同的空间,用来存储一个值.
        Long currentId = BaseContext.getCurrentId();
//        long employee1 = (long) httpServletRequest.getSession().getAttribute("employee");
        metaObject.setValue("createUser",currentId);
        metaObject.setValue("updateUser",currentId);
    }

    //    在更新的时候才会使用
    @Override
    public void updateFill(MetaObject metaObject) {
        long id = Thread.currentThread().getId();
        log.info("线程"+id);
        log.info("公共字段填冲update");
        log.info(metaObject.toString());
        Long currentId = BaseContext.getCurrentId();
        System.out.println(currentId);
        metaObject.setValue("updateUser",currentId);
        metaObject.setValue("updateTime",LocalDateTime.now());
    }
}
